home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / bplus25a.zip / NAMES.C < prev    next >
Text File  |  1990-12-30  |  7KB  |  229 lines

  1. /*******************************************************************/
  2. /*                              NAMES.C                             */
  3. /*                                                                  */
  4. /* This example shows how easy it is to write a program for an      */
  5. /* online address book using the B-PLUS file indexing toolkit.      */
  6. /* The program creates a file of names, addresses, and telephone    */
  7. /* numbers.  A record is displayed on the screen by entering part   */
  8. /* or all of the name.  Although the program is usefully as written */
  9. /* it has purposely been kept simple.  You may want to add new      */
  10. /* features to the progran.                                         */
  11. /*                                                                  */
  12. /********************************************************************/
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <process.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include "bplus.h"
  21.  
  22.  
  23. typedef struct              /* Here is the address record definition */
  24.   {
  25.      char lastname[16];     /* last name           */
  26.      char firstname[16];    /* first name          */
  27.      char address1[31];     /* first address line  */
  28.      char address2[31];     /* second address line */
  29.      char city[21];         /* the city            */
  30.      char state[3];         /* the state           */
  31.      char zipcode[6];       /* postal zip code     */
  32.      char telephone[14];    /* telephone number    */
  33.   }  ADDRESS;
  34.  
  35.  
  36. IX_DESC  nameindex;             /* index file variable  */
  37. FILE     *namefile;             /* data file pointer    */
  38. ADDRESS  person;                /* data record variable */
  39.  
  40.  
  41. void openfiles(void);
  42. void closefiles(void);
  43. int addrecord(void);
  44. void getstring(char*, int);
  45. void newaddress(void);
  46. void printname(ENTRY*);
  47. void getname(void);
  48. void nextname(void);
  49. void listnames(void);
  50.  
  51.  
  52. void openfiles()
  53.   /* If the file NAMES.DAT already exist, open the index and data */
  54.   /* file.  Otherwise, these files are created.                   */
  55.   {
  56.     if ((namefile = fopen("names.dat","r+")) != NULL)
  57.       open_index("names.idx", &nameindex, 1);      /* open index file  */
  58.     else
  59.     {
  60.       namefile = fopen("names.dat","w+");          /* create data file */
  61.       if (namefile == NULL)
  62.         {
  63.           printf("Unable to open namefile\n");
  64.           exit(1);
  65.         }
  66.       make_index("names.idx", &nameindex, 1);   /* creat index file     */
  67.     }                                           /* allow duplicate keys */
  68.   } /* openfiles */
  69.  
  70.  
  71. void closefiles()
  72.   /* close all files and exit */
  73.   {
  74.     fclose(namefile);
  75.     close_index(&nameindex);
  76.     exit(0);
  77.   } /* closefiles */
  78.  
  79.  
  80. int addrecord()
  81.   /* add a new address to the data file - add index to index file */
  82.   {
  83.     ENTRY ee;
  84.     int ret;
  85.     ret = fseek(namefile, 0L, SEEK_END);      /* seek to end of datafile  */
  86.     if (ret == 0)
  87.       {
  88.         strcpy(ee.key, person.lastname);      /* key is last name followed */
  89.         strcat(ee.key, person.firstname);     /* first name.  Capitalize   */
  90.         strupr(ee.key);                       /*    and copy to ee.key.    */
  91.         ee.recptr = ftell(namefile);          /* get position in datafile  */
  92.         if (ee.recptr != -1L)
  93.           {
  94.             if (add_key(&ee, &nameindex) == IX_OK)     /* add key to index */
  95.               {
  96.                 fwrite(&person,sizeof(person),1,namefile);  /* add address */
  97.                 return (IX_OK);
  98.               }
  99.           }
  100.       }
  101.     else printf("Seek error - data file");
  102.     return (IX_FAIL);
  103.   } /* addrecord */
  104.  
  105.  
  106. void getstring(mes, length)
  107.   char *mes;
  108.   int length;
  109.   /* input a string and check that it is not too long   */
  110.   {
  111.     char message[80];
  112.     gets(message);
  113.     if ((int)strlen(message) > length) message[length] = '\0';
  114.     strcpy(mes,message);
  115.   } /* getstring */
  116.  
  117.  
  118. void newaddress()
  119.   /* add new address records */
  120.   {
  121.     while (1)
  122.      {
  123.        printf("\n\nLast Name      : ");
  124.        getstring(person.lastname,15);
  125.        if ( strlen(person.lastname) > 0)       /* quit if no last name */
  126.          {
  127.            printf("First Name     : ");
  128.            getstring(person.firstname,15);
  129.            printf("Address Line 1 : ");
  130.            getstring(person.address1,30);
  131.            printf("Address Line 2 : ");
  132.            getstring(person.address2,30);
  133.            printf("City           : ");
  134.            getstring(person.city,20);
  135.            printf("State          : ");
  136.            getstring(person.state,2);
  137.            printf("Zip Code       : ");
  138.            getstring(person.zipcode,5);
  139.            printf("Telephone      : ");
  140.            getstring(person.telephone,13);
  141.            addrecord();                     /* update data and index files */
  142.            printf("\n");
  143.          }
  144.          else return ;
  145.      }
  146.   } /* newaddress */
  147.  
  148.  
  149. void printname(e)
  150.   ENTRY *e;
  151.   /* retrieve a data record and print it on the screen */
  152.   {
  153.     int ret;
  154.  
  155.     /* seek to the record address stored in ENTRY e->recptr */
  156.     ret = fseek(namefile, e->recptr, SEEK_SET);
  157.  
  158.     if (ret == 0)     /* if OK read the record and display */
  159.       {
  160.         fread(&person,sizeof(person),1,namefile);
  161.         printf("\n\n            %s %s" , person.firstname,person.lastname);
  162.         printf("\n            %s", person.address1);
  163.         if (strlen(person.address2) > 0)
  164.            printf("\n            %s", person.address2);
  165.         printf("\n            %s, %s  %s", person.city,person.state,person.zipcode);
  166.         printf("\n            %s\n", person.telephone);
  167.       }
  168.     else printf("Seek error - data file");
  169.   } /* printname */
  170.  
  171.  
  172. void getname()
  173.   /* Get an address record by entering part or all of name */
  174.   /* Enter last name first then first name with no spaces  */
  175.   {
  176.     ENTRY ee;
  177.     printf("\n\nEnter name: ");
  178.     gets(ee.key);
  179.  
  180.     /* make all upper case letters and copy to ee.key */    
  181.     strupr(ee.key);
  182.  
  183.     /* use locate_key instead of find_key so an exact match not required */
  184.     if (locate_key(&ee, &nameindex) != EOIX) printname(&ee);
  185.     else printf("No key this large in index file\n");
  186.   } /* getname */
  187.  
  188.  
  189. void nextname()
  190.   /* display the next address in the address file */
  191.   {
  192.     ENTRY ee;
  193.     if (next_key(&ee, &nameindex) == IX_OK) printname(&ee);
  194.     else printf("\nEnd of index file\n");
  195.   } /* nextname */
  196.  
  197.  
  198. void listnames()
  199.   /* list all the names in the address file */
  200.   {
  201.     ENTRY ee;
  202.     if (first_key(&ee, &nameindex)) printname(&ee);
  203.     while (next_key(&ee, &nameindex) == IX_OK) printname(&ee);
  204.   } /* listnames */
  205.  
  206. void main()
  207.   /* Here is the main program loop */
  208.   {
  209.     int cmd;
  210.     int  done;
  211.     done = 0;
  212.     openfiles();
  213.     do
  214.      {
  215.        printf("\nCommand: A (Add Name), F (Find), N (Next), L (List), Q (Quit): ");
  216.        cmd = getche();
  217.        switch (toupper(cmd))
  218.         {
  219.           case 'A': newaddress(); break;   /* add a name to address file   */
  220.           case 'F': getname(); break;      /* find an address              */
  221.           case 'N': nextname(); break;     /* display next address in file */
  222.           case 'L': listnames(); break;    /* display all addresses        */
  223.           case 'Q': closefiles();          /* quit and close files         */
  224.         }
  225.       }
  226.     while (!done);
  227.   } /* main */
  228.  
  229.